package com.hospital.hospital.controller;

import com.hospital.hospital.bean.Drug;
import com.hospital.hospital.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xfm
 * @create 2021/9/6
 */
@Controller
@RequestMapping("/drug")
@SessionAttributes(types = {Drug.class})
public class DrugController {
    @Autowired
    public DrugService drugService;

    @RequestMapping("/showUi")
    public String showUi(Model model,Integer page){
        if(page==null){
            page=1;
        }
        try {
            List<Drug> drugs1 = drugService.show();
            List<Drug> drugs=new ArrayList<>();
            if(drugs!=null){
                drugs.clear();
            }
            for (int i=3*page-3;i<=page*3-1;i++){
                if(i==drugs1.size()){break;}
                drugs.add(drugs1.get(i));

            }
            model.addAttribute("drugs",drugs);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "drug";
    }
    @RequestMapping("/delete")
    public String delete(Integer[] id){
        for (int i = 0; i < id.length; i++) {
            try {
                drugService.delete(id[i]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "redirect:/drug/showUi";
    }
    @RequestMapping("/update")
    public String update(Integer id,Integer num,Double sprice){
        try {
            drugService.update(id,num,sprice);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/drug/showUi";
    }
    @RequestMapping("/updateUi")
    public String updateUi(Model model,Integer id){
        try {
            Drug drug = drugService.queryById(id);
            model.addAttribute("drug",drug);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "drugUpdate";
    }
    @RequestMapping("/add")
    public  String add(String dname,Double sprice,Integer num){
        try {
            drugService.add(dname,sprice,num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/drug/showUi";
    }
    @RequestMapping("/buyUi")
    public String buyUi(Model model, HttpServletRequest request){
        try {
            Object user = request.getSession().getAttribute("user");
            request.setAttribute("user",user);
            Integer many = (Integer) request.getSession().getAttribute("many");
            List<Drug> drugs = drugService.show();
            model.addAttribute("drugs",drugs);
            model.addAttribute("many",many);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "drugBuy";
    }

}
